<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="UserObj.photo" @click="$refs.iptFile.click()">
            <input
              type="file"
              ref="iptFile"
              v-show="false"
              accept="image/*"
              @change="onFileChange"
            />
          </van-image>
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="UserObj.name" @click="isNameDialogFn" />
      <van-cell title="生日" is-link :value="UserObj.birthday" @click="isDateDialogFn" />
    </van-cell-group>

    <!-- 用户名弹窗 -->
    <van-dialog
      v-model="isNameShow"
      title="修改名称"
      show-cancel-button
      :beforeClose="beforeCloseFn"
    >
      <!-- 输入框 -->
      <!-- <input type="text" v-fofo v-model.trim="userName" > -->
      <van-field
        v-model.trim="userName"
        input-align="center"
        placeholder="请输入名称"
        v-fofo
      />
    </van-dialog>

    <!-- 时间选择器 -->
    <van-popup v-model="isDateShow" position="bottom" :style="{ height: '50%' }" class="aaa">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="请选择出生日期"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="cancelDateBtn"
        @confirm="confirmDateBtn"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfileAPI, updatePhotoAPI, updeUserProfileAPI } from '@/api'
import { formatDate } from '@/utils/date'
export default {
  name: 'UserEdit',
  data () {
    return {
      UserObj: {}, // 用户信息对象
      isNameShow: false, // 名字弹窗状态
      userName: '', // 用户名
      isDateShow: false, // 时间状态
      minDate: new Date(1990, 0, 1),
      maxDate: new Date(2022, 7, 10),
      currentDate: new Date(2022, 7, 10)
    }
  },
  mounted () {
    this.getUserProfileFn()
  },
  methods: {
    async getUserProfileFn () {
      const res = await getUserProfileAPI()
      // console.log(res)
      this.UserObj = res.data.data
    },
    // 图片上传
    async onFileChange (ev) {
      // console.log(ev.target) 本身标签 input
      // if (ev.target.files.length === 0) return
      const fd = new FormData()
      fd.append('photo', ev.target.files[0])
      const res = await updatePhotoAPI(fd)
      console.log(res)
      this.UserObj.photo = res.data.data.photo
    },
    // 用户名弹窗
    isNameDialogFn () {
      this.isNameShow = true
      this.userName = this.UserObj.name // 设置默认显示内容
    },
    async beforeCloseFn (action, done) {
      // console.log(action)
      if (action === 'cancel') {
        // 取消
        done()
      } else {
        // 确定
        const reg = /^[1-9a-zA-Z\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.userName)) {
          const res = await updeUserProfileAPI({
            name: this.userName
          })
          console.log(res)
          this.UserObj.name = this.userName // 更新成功 - 回显到页面上
          done()
        } else {
          // 没通过校验, 给用户提示
          this.$Notify({ type: 'warning', message: '用户名只能是1-7位中英数字组合' })
          done(false)
        }
      }
    },
    // 时间弹窗
    isDateDialogFn () {
      this.isDateShow = true
      this.currentDate = new Date(this.UserObj.birthday) // 组件显示默认生日
    },
    // 时间取消事件
    cancelDateBtn () {
      this.isDateShow = false
    },
    // 时间完成事件
    async confirmDateBtn () {
      // console.log(formatDate(this.currentDate))
      await updeUserProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.UserObj.birthday = formatDate(this.currentDate)
      this.isDateShow = false
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
// ::v-deep .van-dialog__content {
//   text-align: center;
//   input {
//     padding: 0;
//     outline: none;
//     border: none;
//     text-align: center;
//   }
// }
.aaa {
  border-radius: 0.42667rem 0.42667rem 0 0;
}
</style>
